

import { defineStore } from "pinia"
import store from '@/store'
import { AuthApiFn } from '@/apis'
import mittBus, { MittBusKeys } from '@/mittBus'
import { TStore } from "../typings"

/** 验证用户状态存储 */
export const useAuthStore = defineStore('auth-store', {
    state: (): TStore.AuthStore => ({
        token: undefined,
        userInfo: undefined,
        userRoles: undefined
    }),
    getters: {
        /** 是否有Token */
        hasToken(state): boolean {
            return state.token ? true : false
        },
        /** 是否读取用户信息 */
        hasUser(state) {
            return state.userInfo ? true : false
        },
        // 是否读取用户角色信息
        hasRoles(state) {
            return state.userRoles ? true : false
        }
    },
    actions: {
        /** 登录验证用户 */
        async login(loginParams: TAuth.LoginParams) {
            const { status, data } = await AuthApiFn.loginApi(loginParams)
            if (status == 0) {
                this.token = data
                // 发送总线事件,登录成功事件
                mittBus.emit(MittBusKeys.LOGIN_SUCCESS, data)
                return Promise.resolve(true)
            }
            return Promise.resolve(false)
        },
        /** 退出登录 */
        async logout(outParam: TAuth.LogoutParams) {
            const { status } = await AuthApiFn.logoutApi(outParam)
            if (status === 0) {
                this.token = undefined
                this.userInfo = undefined
                this.userRoles = undefined
                // 发送总线事件,退出登录事件
                mittBus.emit(MittBusKeys.LOGOUT_SUCCESS, undefined)
                return Promise.resolve(true)
            }
            return Promise.resolve(false)
        },
        /** 获取登录用户信息 */
        async getUserInfo(getParams: TAuth.GetUsrInfoParams) {
            const { status, data } = await AuthApiFn.getUserInfoApi(getParams)
            if (status === 0) {
                this.userInfo = data

                return Promise.resolve(true)
            }
            return Promise.resolve(false)
        },
        /** 获取登录用户角色信息 */
        async getUserRoles(params: TAuth.GetUserRolesParams) {
            const { status, data } = await AuthApiFn.getUserRolesApi(params)
            if (status === 0) {
                this.userRoles = data
                mittBus.emit(MittBusKeys.LOGIN_ROLES_SUCCESS, data)
                return Promise.resolve(true)
            }
            return Promise.resolve(false)
        },
        /** 修改密码 */
        async resetPassword(params: TAuth.ResetPasswordParams) {
            const { status } = await AuthApiFn.resetPasswordApi(params)
            return Promise.resolve(status === 0)
        },
        /**
         * 刷新Token
         * @param params {refresh_token: token}
         * @returns 
         */
        async refreshToken(params: TAuth.RefreshTokenParams) {
            const { status, data } = await AuthApiFn.refreshTokenApi(params)
            if (status === 0) {
                this.token = data
                mittBus.emit(MittBusKeys.REFRESH_TOKEN_SUCCESS, data)
                return Promise.resolve(true)
            }
            return Promise.resolve(false)
        }
    }
})

/** Setup 外使用 */
export function useAuthStoreHook() {
    return useAuthStore(store)
}

/** 处理事件 */
mittBus.on(MittBusKeys.LOGIN_SUCCESS, () => {
    const authStore = useAuthStoreHook()
    authStore.getUserInfo({})
    authStore.getUserRoles({})
})